package com.example.mall.web.controller;

import com.example.mall.domain.User;
import com.example.mall.dto.RegisterDto;
import com.example.mall.dto.UserDto;
import com.example.mall.exception.ErrorEnumCode;
import com.example.mall.exception.GlobalCustomerException;
import com.example.mall.query.PageDTO;
import com.example.mall.query.UserQuery;
import com.example.mall.result.JSONResult;
import com.example.mall.service.IUserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    public IUserService userService;

    /*
     * @Description:  用户注册
     * @param null
     * @return:
     * @Author:  xxxli
     * @date:  2025/5/14 10:26
     */

    @RequestMapping(value = "/register",method = RequestMethod.POST)
    @ApiOperation(value = "用户注册",notes = "用户注册",httpMethod = "POST")
    public JSONResult register(@RequestBody RegisterDto registerDto){
        userService.register(registerDto);
        return JSONResult.success();
    }

    /**
     * 分页查询用户基本信息表信息
     * @param userQuery
     * @return
     */
    @GetMapping("queryPage")
    @ApiOperation(value = "分页查询用户基本信息表信息",notes = "分页查询用户基本信息表信息",httpMethod = "GET")
    public JSONResult queryPage(UserQuery userQuery){
        PageDTO<UserDto> userDtoPageDTO = userService.queryPage(userQuery);
        return JSONResult.success("查询成功",userDtoPageDTO);
    }

    /**
     * 根据id查询用户基本信息表信息
     * @param id
     * @return
     */
    @GetMapping("query/{id}")
    @ApiOperation(value = "根据id查询用户基本信息表信息",notes = "根据id查询用户基本信息表信息",httpMethod = "GET")
    public JSONResult query(@PathVariable("id")Long id){
        //1.根据id查询用户信息
        if (id == null){
            throw new GlobalCustomerException(ErrorEnumCode.USERID_NOT_ID);
        }
        User user = userService.getById(id);
        return JSONResult.success("查询成功",user);
    }

    /**
     * 根据id查询用户基本信息表信息
     * @param id
     * @return
     */
    @GetMapping("queryUserById/{id}")
    @ApiOperation(value = "根据id查询用户基本信息表信息",notes = "根据id查询用户基本信息表信息",httpMethod = "GET")
    public JSONResult queryUserById(@PathVariable("id")Long id){
        //1.根据id查询用户信息
        if (id == null){
            throw new GlobalCustomerException(ErrorEnumCode.USERID_NOT_ID);
        }
        User user = userService.queryUserById(id);
        return JSONResult.success("查询成功",user);
    }

    /**
     * 查询全部用户基本信息表信息
     * @param
     * @return
     */
    @GetMapping("queryUserList")
    @ApiOperation(value = "查询全部用户基本信息表信息",notes = "查询全部用户基本信息表信息",httpMethod = "GET")
    public JSONResult queryUserList(){
        List<User> logins = userService.queryUserList();
        return JSONResult.success("查询成功",logins);
    }

    /**
     * 新增用户基本信息表
     * @param userDto
     * @return
     */
    @PostMapping("insert")
    @ApiOperation(value = "新增用户基本信息表",notes = "新增用户基本信息表",httpMethod = "POST")
    public JSONResult insert(@RequestBody UserDto userDto){
        userService.insert(userDto);
        return JSONResult.success("保存成功");
    }

    /**
     * 修改用户基本信息表信息
     * @param userDto
     * @return
     */
    @PostMapping("update")
    @ApiOperation(value = "修改用户基本信息表信息",notes = "修改用户基本信息表信息",httpMethod = "POST")
    public JSONResult update(@RequestBody UserDto userDto){
        userService.update(userDto);
        return JSONResult.success("修改成功");
    }

    /**
     * 删除用户基本信息表信息
     * @param id
     * @return
     */
    @PostMapping("delete/{id}")
    @ApiOperation(value = "删除用户基本信息表信息",notes = "删除用户基本信息表信息",httpMethod = "POST")
    public JSONResult delete(@PathVariable("activitiesProductId") Long id) {
        userService.delete(id);
        return JSONResult.success("删除成功");
    }

    /**
     * 批量删除用户基本信息表信息
     * @param ids
     * @return
     */
    @PostMapping("deleteBatch")
    @ApiOperation(value = "批量删除用户基本信息表信息",notes = "批量删除用户基本信息表信息",httpMethod = "POST")
    public JSONResult deleteAll(@RequestBody List<Long> ids) {
        userService.deleteBatch(ids);
        return JSONResult.success("批量删除成功");
    }
}
